package com.itcam.tingshu.common.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.CompletableFuture;

/**
 * @author : Cammy.Wu
 * Description : 
 */

@Service
public class KafkaService {
    private static final Logger logger = LoggerFactory.getLogger(KafkaService.class);

    @Autowired
    private KafkaTemplate kafkaTemplate;

    public void sendMsg(String topics, String value){
        // 发送消息
        CompletableFuture completableFuture = kafkaTemplate.send(topics, value);
        // 需要判断消息是否发送成功
        completableFuture.whenComplete((o,throwable)->{
            if(throwable==null){
                logger.info("消息发送成功");
            }else{
                logger.info("消息发送失败");
            }
        });

    }
}
